import random

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View
from django_redis import get_redis_connection
# from rest_framework.generics import CreateAPIView, RetrieveUpdateAPIView, UpdateAPIView, RetrieveAPIView, ListAPIView
# from rest_framework.permissions import IsAuthenticated
# from rest_framework.response import Response
# from rest_framework.views import APIView
# from users import serializers
# from question.models import Label


class MessageView(View):
    #获取短信验证码功能
    def get(self, request, mobile):
        conn = get_redis_connection('verify_code')
        send_flag = conn.get('flag_%s'%mobile)
        if send_flag:
            return JsonResponse({"success":"False","message":'发送失败'})
        sms_code="%06d"%random.randint(0,999999)
        print(sms_code)
        pl=conn.pipeline()
        pl.setex(mobile,300,sms_code)
        pl.setex('flag_%s'%mobile,60,1)
        pl.execute()
        # ccp_send_sms_code.delay(mobile,msg)
        return JsonResponse({"success":"True","message":'发送成功',"sms_code":sms_code})


